package com.hongyi.service.Impl;

import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.hongyi.mapper.DepMapper;
import com.hongyi.pojo.Dep;
import com.hongyi.pojo.PageBean;
import com.hongyi.service.DepService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hongyi
 * @date 2023/11/22 21:14
 */
@Service
public class DepServiceImpl implements DepService {
    @Autowired
    private DepMapper depMapper;

    @Override
    public void add(Dep dep) {
        depMapper.add(dep);
    }

    @Override
    public void update(Dep dep) {
        depMapper.update(dep);
    }

    @Override
    public void delete(int id) {
        depMapper.delete(id);
    }

    @Override
    public Dep findById(int id) {
        return depMapper.findById(id);
    }

    @Override
    public List<Dep> deptByIdPersonId(int id) {
        return depMapper.deptByIdPersonId(id);
    }

    @Override
    public List<Dep> getNameAndAddress(Dep dep) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("name", dep.getName());
        map.put("address", dep.getAddress());
        List<Dep> nameAndAddress = depMapper.getNameAndAddress(map);
        return nameAndAddress;
    }

    @Override
    public Map<String, Object> getMapId(Long id) {
        return depMapper.getMapId(id);
    }

    @Override
    public Map<String, Object> getMapDep(Dep dep) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", dep.getId());
        return depMapper.getMapDep(map);
    }

    /**
     *
     * @param currentPage 当前页
     * @param PageSize 每页条数
     * @return
     */
    @Override
    public PageBean<Dep> selectByPage(int currentPage, int PageSize) {
        int begin = (currentPage - 1) * PageSize;
        int size = PageSize;
        // 查询当前页面内容
        List<Dep> rows = depMapper.selectPage(begin, size);
        int totalcount = depMapper.selectTotalCount();

        // 封装一下分页页面
        PageBean<Dep> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalcount);
        return pageBean;
    }

    @Override
    public PageBean<Dep> selectByPage01(String name, int currentPage, int PageSize) {
        // 4. 计算开始索引
        int begin = (currentPage -1) * PageSize;
        // 计算查询条目数
        int size = PageSize;

        // 查询当前页面内容
        List<Dep> rows = depMapper.selectPage01(name, currentPage, PageSize);

        // 查询总记录数
        int totalCount01 = depMapper.selectTotalCount01(name);

        // 封装分页
        PageBean<Dep> depPageBean = new PageBean<>();
        depPageBean.setRows(rows);
        depPageBean.setTotalCount(totalCount01);
        return depPageBean;
    }


}
